﻿using Common;
using IdentityServer4.EntityFramework.DbContexts;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Repository
{
    public class UnitOfWork<T> : IUnitOfWork<T> where T : DbContext
    {
        private T _db = null;
        public T GetDbContext()
        {
            if (_db == null)
            {
                _db = StaticInfo.GetService<T>();
            }
            return _db;
        }
        public async Task SaveChangesAsync()
        {
            if (_db !=null )
            {
                await _db.SaveChangesAsync();
            }
        }
        public void SaveChanges()
        {
            if (_db != null)
            {
                _db.SaveChanges();
            }
        }
    }
}
